package com.zzh.dp;

import java.util.Arrays;

public class No309_maxProfit {
    public static void main(String[] args) {
        No309Solution solution = new No309Solution();
        int i = solution.maxProfit(new int[]{1, 2, 3, 0, 2});
        System.out.println(i);
    }
}

class No309Solution {
    public int maxProfit(int[] prices) {
        int len = prices.length;
        if (len<2){
            return 0;
        }
        int[][] f = new int[len][3];
        f[0][0] = -prices[0];//持有非冻结
        f[0][1] = 0;//不持有非冻结
        f[0][2] = 0;//不持有冻结
        for (int i = 1; i < len; i++) {
            //状态转移
            f[i][0] = max(f[i - 1][0],  f[i - 1][1] - prices[i]);
            f[i][1]=max(f[i-1][1],f[i-1][2]);
            f[i][2]=f[i-1][0]+prices[i];
        }
//        for (int i=0;i<len;i++){
//            System.out.println(Arrays.toString(f[i]));
//        }
        return max(f[len - 1][1], f[len - 1][2],0);
    }

    public int max(int a, int b) {
        return Math.max(a, b);
    }

    public int max(int a, int b, int c) {
        return Math.max(Math.max(a, b), c);
    }
}